package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.core.util.TypeConvertUtil;
import cn.e4j.modular.api.service.Gwat151Service;
import cn.e4j.modular.api.service.GwaService;
import cn.e4j.modular.api.service.Gwat161Service;
import cn.e4j.modular.api.service.GwbService;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.crm.entity.*;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.beetl.ext.fn.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by wenchuan.li on 2018/06/14.
 */
@RestController
@RequestMapping("api/wx/gwat161")
public class Gwat161Controller {

    @Autowired
    private Gwat161Service gwat161Service;

    @Autowired
    SystemServiceImpl systemService;
    @Autowired
    private Gwat151Service gwat151Service;

    @Autowired
    GwaService gwaService;
    @Autowired
    GwbService gwbService;

    /**
     * 拜访记录提报作业--获取拜访记录列表
     *
     * @return
     * @author wenchuan.li on 2018/06/14 10:13
     * @update wenchuan.li on 2018/06/14 10:15
     */
    @RequestMapping(value = "getGwat161List")
    public ResponseData getServiceOrderList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();

        inDto.put("GWBHENT", inDto.getString("ENT"));
        //inDto.put("gwbhacti", "Y");
        inDto.put("GWBH002", currentUser.getID());
        inDto.put("ID", currentUser.getID());

        //客户权限
//            Dto gwb001 = gwbService.getGwb001(inDto);
//            inDto.put("customers", gwb001.get("customers"));

        //上下级权限
        Dto sys003 = systemService.getSys003(currentUser);
        inDto.put("ORGS", sys003.get("ORGS"));

        //业务员权限
        Dto sys004 = systemService.getSys004(currentUser, (List<Dto>) sys003.get("ORGS"));
        inDto.put("YWY", sys004.get("YWY"));


        return new SuccessResponseData(InvokeUtil.invokeBeanMethod("gwat161Mapper,getGwat161ListPage", wapper.getPage(), inDto));
    }

    /**
     * 拜访记录--新增
     *
     * @return
     * @author wenchuan.li on 2018/06/14 11:44
     */
    @RequestMapping(value = "gwat161Add")
    public ResponseData orderSave(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        GwbhT gwbh_tPO = null;
        List<GwbkT> gwbk_tPOs = new ArrayList<>();
        List<GwbkT> gwbk_tPO1s = new ArrayList<>();
        List<GwblT> gwbl_tPOs = new ArrayList<>();

        try {
            JSONObject inJson = JSONObject.parseObject(inDto.getString("_jsonStr_"));

            JSONObject data = inJson.getJSONObject("data");
            Map<String, Object> map = data;
            gwbh_tPO = JSON.parseObject(data.toJSONString(), GwbhT.class);

            gwbh_tPO.setGWBHENT(currentUser.getDBC());
            gwbh_tPO.setGWBH002(currentUser.getID());
            gwbh_tPO.setGWBHID(data.getString("GWAK001"));
            gwbh_tPO.setGWBHTIME(DateUtils.getTime());
            //gwbh_tPO.setGWBHDATE(new Date());
            gwbh_tPO.setGWBHDATE(LocalDate.now());
            gwbh_tPO.setGWBHUSER(currentUser.getID());
            gwbh_tPO.setGWBHORIG(currentUser.getORG_ID());
            gwbh_tPO.setGWBHGRUP(currentUser.getORG_ID());

            if (ToolUtil.isNotEmpty(data.getString("GWBH019"))) {
                //转成Timestamp 不然时间存入的时候是00:00:00
                Timestamp s1 = (Timestamp) TypeConvertUtil.convert(data.getString("GWBH019"), "Timestamp", "yyyy-MM-dd HH:mm:ss");
                gwbh_tPO.setGWBH019(s1.toLocalDateTime());
            }
            if (ToolUtil.isNotEmpty(data.getString("GWBH020"))) {
                Timestamp s2 = (Timestamp) TypeConvertUtil.convert(data.getString("GWBH020"), "Timestamp", "yyyy-MM-dd HH:mm:ss");
                gwbh_tPO.setGWBH020(s2.toLocalDateTime());
            }

            gwbh_tPO.setGWBHACTI("N");

            // 项次+1
            Dto gwbhMaxDto = Dtos.newDto();
            gwbhMaxDto.put("GWBHENT", inDto.getString("ENT"));
            gwbhMaxDto.put("GWBHNAME", gwbh_tPO.getGWBHNAME());
            gwbhMaxDto.put("GWBHID", gwbh_tPO.getGWBHID());
            List<Dto> maxlist = gwat161Service.getGwbhseqMax(gwbhMaxDto);
            if (maxlist.size() > 0 && ToolUtil.isNotEmpty(maxlist.get(0))) {
                Dto maxDto = maxlist.get(0);
                gwbh_tPO.setGWBHSEQ(maxDto.getBigDecimal("GWBHSEQ").add(BigDecimal.ONE));
            } else {
                gwbh_tPO.setGWBHSEQ(BigDecimal.ONE);
            }


            Dto gwbkMaxDto = Dtos.newDto();
            gwbkMaxDto.put("GWBKENT", inDto.getString("ENT"));
            gwbkMaxDto.put("GWBKACTI", "Y");
            gwbkMaxDto.put("GWBKNAME", gwbh_tPO.getGWBHNAME());
            gwbkMaxDto.put("GWBKID", gwbh_tPO.getGWBHID());
            gwbkMaxDto.put("GWBKSEQ2", gwbh_tPO.getGWBHSEQ());
            List<Dto> gwbkMaxlist = gwat161Service.getGwbkseqMax(gwbkMaxDto);
            BigDecimal gwbkMaxNum;
            if (gwbkMaxlist.size() > 0 && ToolUtil.isNotEmpty(gwbkMaxlist.get(0))) {
                Dto maxDto = gwbkMaxlist.get(0);
                gwbkMaxNum = maxDto.getBigDecimal("GWBKSEQ");
            } else {
                gwbkMaxNum = BigDecimal.ZERO;
            }
            JSONArray orders = inJson.getJSONArray("orders");
            for (int i = 0; i < orders.size(); i++) {
                JSONObject item = orders.getJSONObject(i);
                GwbkT gwbk_tPO = new GwbkT();
                gwbk_tPO.setGWBKENT(currentUser.getDBC());
                gwbk_tPO.setGWBKNAME(gwbh_tPO.getGWBHNAME());
                gwbk_tPO.setGWBKID(data.getString("GWAK001"));
                gwbk_tPO.setGWBKSEQ(gwbkMaxNum.add(new BigDecimal(i + 1)));
                gwbk_tPO.setGWBKSEQ2(gwbh_tPO.getGWBHSEQ());

                gwbk_tPO.setGWBK001(item.getString("GWAM001"));
                gwbk_tPO.setGWBK002(item.getString("GWAM008"));
                gwbk_tPO.setGWBK003(item.getBigDecimal("GWCM003"));
                gwbk_tPO.setGWBK004(item.getBigDecimal("GWAP100"));
                gwbk_tPO.setGWBK005(gwbk_tPO.getGWBK003().multiply(gwbk_tPO.getGWBK004()));
                gwbk_tPO.setGWBK008(item.getString("GWBK008"));
                gwbk_tPO.setGWBK009(item.getString("GWBK009"));
                gwbk_tPO.setGWBKACTI("Y");
                gwbk_tPO.setGWBKDATE(LocalDate.now());
                gwbk_tPO.setGWBKTIME(DateUtils.getTime());
                gwbk_tPO.setGWBKUSER(currentUser.getID());
                gwbk_tPO.setGWBKORIG(currentUser.getORG_ID());
                gwbk_tPO.setGWBKORIU(currentUser.getORG_ID());
                gwbk_tPO.setGWBKGRUP(currentUser.getORG_ID());
                gwbk_tPO.setGWBKMODU(currentUser.getID());

                gwbk_tPOs.add(gwbk_tPO);
            }

            //已经有的订单--也做新增
            JSONArray oldorders = inJson.getJSONArray("oldorders");
            for (int i = 0; i < oldorders.size(); i++) {
                JSONObject item = oldorders.getJSONObject(i);
                GwbkT gwbk_tPO = new GwbkT();

                gwbk_tPO.setGWBKENT(currentUser.getDBC());
                gwbk_tPO.setGWBKNAME(gwbh_tPO.getGWBHNAME());
                gwbk_tPO.setGWBKID(data.getString("GWAK001"));
                gwbk_tPO.setGWBKSEQ(item.getBigDecimal("GWBKSEQ"));
                gwbk_tPO.setGWBKSEQ2(gwbh_tPO.getGWBHSEQ());
                gwbk_tPO.setGWBK001(item.getString("GWBK001"));
                gwbk_tPO.setGWBK002(item.getString("GWBK002"));
                gwbk_tPO.setGWBK003(item.getBigDecimal("GWBK003"));
                gwbk_tPO.setGWBK004(item.getBigDecimal("GWBK004"));
                gwbk_tPO.setGWBK005(gwbk_tPO.getGWBK003().multiply(gwbk_tPO.getGWBK004()));
                gwbk_tPO.setGWBK008(item.getString("GWBK008"));
                gwbk_tPO.setGWBK009(item.getString("GWBK009"));
                gwbk_tPO.setGWBKACTI("Y");
                gwbk_tPO.setGWBKDATE(LocalDate.now());
                gwbk_tPO.setGWBKUSER(currentUser.getID());
                gwbk_tPO.setGWBKORIG(currentUser.getORG_ID());
                gwbk_tPO.setGWBKGRUP(currentUser.getORG_ID());
                gwbk_tPO.setGWBKMODU(currentUser.getID());
                gwbk_tPOs.add(gwbk_tPO);
            }

            //评估项
            JSONArray pgxs = inJson.getJSONArray("pgx");
            for (int i = 0; i < pgxs.size(); i++) {
                JSONObject item = pgxs.getJSONObject(i);
                GwblT gwbl_tPO = new GwblT();

                gwbl_tPO.setGWBLENT(currentUser.getDBC());
                gwbl_tPO.setGWBLNAME(gwbh_tPO.getGWBHNAME());
                gwbl_tPO.setGWBLID(data.getString("GWAK001"));
                gwbl_tPO.setGWBLSEQ(new BigDecimal(i + 1));

                if (ToolUtil.isNotEmpty(item.getString("old"))) {
                    gwbl_tPO.setGWBL001(item.getString("GWBL001"));
                    gwbl_tPO.setGWBL002(item.getString("GWBL002"));
                } else {
                    gwbl_tPO.setGWBL001(item.getString("GWBC001"));
                    gwbl_tPO.setGWBL002(item.getString("GWBC002"));
                }
                gwbl_tPO.setGWBLACTI("Y");
                gwbl_tPO.setGWBLDATE(LocalDate.now());
                gwbl_tPO.setGWBLUSER(currentUser.getID());
                gwbl_tPO.setGWBLORIG(currentUser.getORG_ID());

                gwbl_tPOs.add(gwbl_tPO);
            }

            //下次拜访有数据时添加到 人员日程安排里面
            //add by wenchuan.li on 2018/10/16 15:42
            GwbaT gwba_tPO = new GwbaT();
            gwba_tPO.setGWBAENT(currentUser.getDBC());
            gwba_tPO.setGWBAID(currentUser.getID());

            // 项次+1
            inDto.put("GWBAENT", inDto.getString("ENT"));
            inDto.put("GWBAACTI", "Y");
            inDto.put("GWBAID", currentUser.getID());
            List<Dto> maxGwbalist = gwat151Service.getTaskMax(inDto);
            if (maxGwbalist.size() > 0 && ToolUtil.isNotEmpty(maxGwbalist.get(0))) {
                Dto maxDto = maxGwbalist.get(0);
                gwba_tPO.setGWBASEQ(maxDto.getBigDecimal("GWBASEQ").add(BigDecimal.ONE));
            } else {
                gwba_tPO.setGWBASEQ(BigDecimal.ONE);
            }

            gwba_tPO.setGWBA001(gwbh_tPO.getGWBH025());//事项
            gwba_tPO.setGWBA002("Y");//全天Y/N
            //gwba_tPO.setGWBADOCDATE(new Date());
            gwba_tPO.setGWBADOCDATE(LocalDate.now());
            gwba_tPO.setGWBA003(data.getString("GWBH019"));//开始时间
            gwba_tPO.setGWBA004(data.getString("GWBH020"));//结束时间
            gwba_tPO.setGWBA005(gwbh_tPO.getGWBH024());//地址
            gwba_tPO.setGWBA006(gwbh_tPO.getGWBH026());//拜访内容
            gwba_tPO.setGWBA007("1");//1.工作、2.生活
            gwba_tPO.setGWBA008("1");//0.自建、1.导入
            gwba_tPO.setGWBA009("0");//0.不重复 1.每天 2.每周 3.每两周 4.每月 5.每年
            gwba_tPO.setGWBA010("0");//0.不结束 1.定于日期
            gwba_tPO.setGWBA012(BigDecimal.ONE);
            gwba_tPO.setGWBA014("1");//1.正忙、2.空闲
            gwba_tPO.setGWBA015(gwbh_tPO.getGWBHID());//拜访客户编号
            gwba_tPO.setGWBA016(gwbh_tPO.getGWBH021());//拜访人员姓名
            gwba_tPO.setGWBA017(gwbh_tPO.getGWBH022());//职位编号
            gwba_tPO.setGWBA018(gwbh_tPO.getGWBH023());//角色编号
            gwba_tPO.setGWBA019(gwbh_tPO.getGWBHNAME());//商机主题
            gwba_tPO.setGWBA020(gwbh_tPO.getGWBHSEQ()+"");//项次

            gwba_tPO.setGWBAACTI("Y");
            gwba_tPO.setGWBAUSER(currentUser.getID());
            gwba_tPO.setGWBAORIU(currentUser.getID());
            gwba_tPO.setGWBAORIG(currentUser.getORG_ID());
            gwba_tPO.setGWBAGRUP(currentUser.getORG_ID());

            Date date = new Date();
            gwba_tPO.setGWBADATE(LocalDate.now());
            gwba_tPO.setGWBATIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));

            GwbeT gwbe_tPO = new GwbeT();
            gwbe_tPO.setGWBEENT(currentUser.getDBC());
            gwbe_tPO.setGWBE001(gwbh_tPO.getGWBHNAME());
            gwbe_tPO.setGWBE002(gwbh_tPO.getGWBHID());
            gwbe_tPO.setGWBE003(gwbh_tPO.getGWBH035());
            gwbe_tPO.setGWBEMODU(currentUser.getID());
            gwat161Service.gwat161Add(currentUser, gwbh_tPO, gwbk_tPOs, gwbl_tPOs, gwbk_tPO1s, gwba_tPO, gwbe_tPO);

        } catch (Exception e) {

            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData();
    }


    /**
     * 拜访记录--获取拜访人
     *
     * @return
     * @author wenchuan.li on 2018/07/3 13:47
     */
    @RequestMapping(value = "gwat161Bfr")
    public ResponseData gwat161Bfr(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        inDto.put("GWBIENT", inDto.getString("ENT"));
        inDto.put("GWBIACTI", "Y");

        //排序
//        Dto sort = Dtos.newDto();
//        sort.put("property", "gwbidate");
//        sort.put("direction", "desc");
//        sortList.add(sort);
//        inDto.put("sort", AppJson.toGridJson(sortList));

        return new SuccessResponseData(gwat161Service.getBfrPage(wapper.getPage(), inDto));
    }

    /**
     * 拜访人--新增
     *
     * @return
     * @author wenchuan.li on 2018/07/04 13:24
     */
    @RequestMapping(value = "bfrAdd")
    public ResponseData bfrAdd(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        Dto outDto = new Dto();
        GwbiT gwbi_tPO = new GwbiT();
        GwalT gwal_tPO = new GwalT();

        try {

            inDto.put("GWBIENT", inDto.getString("ENT"));
            inDto.put("GWBIACTI", "Y");

            gwbi_tPO = JSON.parseObject(JSON.toJSONString(inDto), GwbiT.class);
            gwbi_tPO.setGWBIENT(inDto.getString("ENT"));
            gwbi_tPO.setGWBIACTI("Y");


            //根据 主题+客户+有效Y 查数据库是否存在，存在就延用已经有id, 项次+1
            List<Dto> maxlist = gwat161Service.getGwahMaxSeq(inDto);
            if (maxlist.size() > 0 && ToolUtil.isNotEmpty(maxlist.get(0))) {
                Dto maxDto = maxlist.get(0);
                gwbi_tPO.setGWBISEQ((maxDto.getBigDecimal("GWBISEQ").add(BigDecimal.ONE)));
            } else {
                gwbi_tPO.setGWBISEQ(BigDecimal.ONE);
            }
            gwbi_tPO.setGWBIDATE(LocalDate.now());
            gwbi_tPO.setGWBIORIU(currentUser.getID());
            gwbi_tPO.setGWBITIME(DateUtils.getTime());
            gwbi_tPO.setGWBIORIG(currentUser.getORG_ID());
            gwbi_tPO.setGWBIGRUP(currentUser.getORG_ID());
            gwbi_tPO.setGWBIMODU(currentUser.getID());
            gwbi_tPO.setGWBIUSER(currentUser.getID());

            // 拜访人新增时 同时 添加到联系人表
            // add by wenchuan.li 2018/11/07 17:30
            Dto dto = new Dto();
            dto.put("GWAK001", gwbi_tPO.getGWBIID());
            dto.put("GWAKENT", inDto.getString("ENT"));
            Dto gwakDto = gwaService.getGwakBygwak001(dto);

            gwal_tPO.setGWALUSER(currentUser.getID());
            gwal_tPO.setGWALMODU(currentUser.getID());
            gwal_tPO.setGWALORIU(currentUser.getID());
            gwal_tPO.setGWALGRUP(currentUser.getORG_ID());
            gwal_tPO.setGWALORIG(currentUser.getORG_ID());
            gwal_tPO.setGWALDATE(LocalDate.now());
            gwal_tPO.setGWALTIME(DateUtils.getTime());

            gwal_tPO.setGWALENT(currentUser.getDBC());
            gwal_tPO.setGWAL001(gwbi_tPO.getGWBIID());

            Dto inDto2 = new Dto();
            inDto2.put("GWAL001", gwbi_tPO.getGWBIID());
            inDto2.put("GWALENT", inDto.getString("ENT"));
            gwal_tPO.setGWAL002(gwaService.getGwa002(inDto2));

            gwal_tPO.setGWAL003("N");
            gwal_tPO.setGWAL010(gwakDto.getString("GWAK020"));
            gwal_tPO.setGWAL011(gwakDto.getString("GWAK021"));
            gwal_tPO.setGWAL012(gwakDto.getString("GWAK022"));
            gwal_tPO.setGWAL013(gwakDto.getString("GWAK023"));

            gwal_tPO.setGWAL014(gwakDto.getString("GWAK024"));
            gwal_tPO.setGWAL015(gwakDto.getString("GWAK025"));
            gwal_tPO.setGWAL016(ToolUtil.isNotEmpty(gwakDto.getString("GWAK026")) ? gwakDto.getString("GWAK026") : "*");
            gwal_tPO.setGWAL017(ToolUtil.isNotEmpty(gwakDto.getString("GWAK027")) ? gwakDto.getString("GWAK027") : "*");
            gwal_tPO.setGWAL018(ToolUtil.isNotEmpty(gwakDto.getString("GWAK028")) ? gwakDto.getString("GWAK028") : "*");
            gwal_tPO.setGWAL019(ToolUtil.isNotEmpty(gwakDto.getString("GWAK029")) ? gwakDto.getString("GWAK029") : "*");
            gwal_tPO.setGWAL020(gwbi_tPO.getGWBI003());
            gwal_tPO.setGWAL021(gwbi_tPO.getGWBI002());
            gwal_tPO.setGWALACTI("Y");

            gwat161Service.bfrAdd(gwbi_tPO, gwal_tPO);

            outDto.put("GWBINAME", gwbi_tPO.getGWBINAME());
        } catch (Exception e) {
            outDto.setAppCode(500);
            e.printStackTrace();
            return new ErrorResponseData("服务开小差了");
        }
        return new SuccessResponseData(outDto);
    }

    /**
     * 拜访记录--获取拜访人
     *
     * @return
     * @author wenchuan.li on 2018/10/26 16:14
     */
    @RequestMapping(value = "gwat161Bfr01")
    public ResponseData gwat161Bfr01(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        inDto.put("GWBIENT", inDto.getString("ENT"));
        inDto.put("GWBIACTI", "Y");
        return new SuccessResponseData(gwat161Service.getBfrPage01(wapper.getPage(), inDto));
    }

    /**
     * 根据客户id获取 商机主题
     *
     * @return
     * @author wenchuan.li on 2018/07/11 13:50
     */
    @RequestMapping(value = "getSjztListByGwbg002")
    public ResponseData getSjztListByGwbg002(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        SysUser user = wapper.getSysUser();
        try {
            inDto.put("GWBGENT", inDto.getString("ENT"));
            inDto.put("GWBGACTI", "Y");
            //排序
//            Dto sort = Dtos.newDto();
//            sort.put("property", "gwbgdate");
//            sort.put("direction", "desc");
//            sortList.add(sort);
//            inDto.put("sort", AppJson.toGridJson(sortList));

            //上下级权限
            Dto sys003 = systemService.getSys003(user);
            inDto.put("ORGS", sys003.get("ORGS"));

            //业务员权限
            Dto sys004 = systemService.getSys004(user, (List<Dto>) sys003.get("ORGS"));
            inDto.put("YWY", sys004.get("YWY"));
            list = gwat161Service.getSjztListByGwbg002(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData(list);
    }
//

    /**
     * 根据主题+客户获取已有的 评估值
     *
     * @return
     * @author wenchuan.li on 2018/07/12 15:52
     */
    @RequestMapping(value = "getPgz")
    public ResponseData getPgz(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();

        inDto.put("GWBLENT", inDto.getString("ENT"));
        inDto.put("GWBLACTI", "Y");

        return new SuccessResponseData(gwat161Service.getPgz(inDto));
    }
//

    /**
     * 根据主题 获取已有的 竞争信息
     *
     * @return
     * @author wenchuan.li on 2018/07/12 17:13
     */
    @RequestMapping(value = "getJzxx")
    public ResponseData getJzxx(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();

        inDto.put("GWBJENT", inDto.getString("ENT"));
        inDto.put("GWBJACTI", "Y");

        return new SuccessResponseData(gwat161Service.getJzxx(inDto));
    }

    /**
     * 竞争信息--新增
     *
     * @return
     * @author wenchuan.li on 2018/07/13 09:34
     */
    @RequestMapping(value = "jzxxAdd")
    public ResponseData jzxxAdd(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        GwbjT gwbj_tPO = new GwbjT();
        SysUser currentUser = wapper.getSysUser();
        try {
            inDto.put("GWBJENT", inDto.getString("ENT"));
            inDto.put("GWBJACTI", "Y");

            gwbj_tPO = JSON.parseObject(JSON.toJSONString(inDto), GwbjT.class);

            gwbj_tPO.setGWBJENT(inDto.getString("ENT"));
            gwbj_tPO.setGWBJACTI("Y");

            // 项次+1
            List<Dto> maxlist = gwat161Service.getJzxxMax(inDto);
            if (maxlist.size() > 0 && ToolUtil.isNotEmpty(maxlist.get(0))) {
                Dto maxDto = maxlist.get(0);
                gwbj_tPO.setGWBJSEQ(maxDto.getBigDecimal("GWBJSEQ").add(BigDecimal.ONE));
            } else {
                gwbj_tPO.setGWBJSEQ(BigDecimal.ONE);
            }
            gwbj_tPO.setGWBJDATE(LocalDate.now());
            gwbj_tPO.setGWBJORIU(currentUser.getID());
            gwbj_tPO.setGWBJTIME(DateUtils.getTime());

            gwbj_tPO.insert();

        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData();
    }

    /**
     * 竞争信息--修改
     *
     * @return
     * @author wenchuan.li on 2018/07/13 10:03
     */
    @RequestMapping(value = "jzxxEdit")
    public ResponseData jzxxEdit(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        GwbjT gwbj_tPO = new GwbjT();
        SysUser currentUser = wapper.getSysUser();
        try {
            inDto.put("GWBJENT", inDto.getString("ENT"));
            //inDto.put("gwbjacti", "Y");
            gwbj_tPO = JSON.parseObject(JSON.toJSONString(inDto), GwbjT.class);

            gwbj_tPO.setGWBJMODU(currentUser.getID());

            gwat161Service.jzxxEdit(gwbj_tPO);

        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData();
    }

    /**
     * 根据主题+客户获取已有的 商品明细
     *
     * @return
     * @author wenchuan.li on 2018/07/17 17:16
     */
    @RequestMapping(value = "gwat161Spmx")
    public ResponseData gwat161Spmx(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> result = new ArrayList<>();
        try {
            inDto.put("GWBKENT", inDto.getString("ENT"));
            inDto.put("GWBKACTI", "Y");
            String GWBHSEQ_ = inDto.getString("GWBHSEQ");
            if (ToolUtil.isNotEmpty(GWBHSEQ_)) {
                inDto.put("GWBKSEQ2", GWBHSEQ_);
            } else {
                List<Dto> gwbkMaxlist = gwat161Service.getGwbkseq2Max(inDto);
                if (gwbkMaxlist.size() > 0 && ToolUtil.isNotEmpty(gwbkMaxlist.get(0))) {
                    Dto maxDto = gwbkMaxlist.get(0);
                    inDto.put("GWBKSEQ2", maxDto.getBigDecimal("GWBKSEQ"));
                }
            }
            if (ToolUtil.isNotEmpty(inDto.getString("GWBKSEQ2"))) {
                result = gwat161Service.gwat161Spmx(inDto);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData(result);
    }

    /**
     * 根据主键删除 商品明细
     *
     * @return
     * @author wenchuan.li on 2018/07/17 10:14
     */
    @RequestMapping(value = "delGwbkByKey")
    public ResponseData delGwbkByKey(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        try {

            inDto.put("GWBKENT", inDto.getString("ENT"));
            inDto.put("GWBKACTI", "Y");

            GwbkT gwbkT = JSON.parseObject(JSON.toJSONString(inDto), GwbkT.class);
            gwbkT.deleteById();
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData();
    }


    /**
     * 拜访记录--修改
     *
     * @return
     * @author wenchuan.li on 2018/07/18 11:34
     */
    @RequestMapping(value = "gwat161Edit")
    public ResponseData gwat161Edit(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        GwbhT gwbh_tPO = null;
        GwblT delGwblT = null;
        List<GwbkT> gwbk_tPOs = new ArrayList<>();
        List<GwbkT> gwbk_tPO1s = new ArrayList<>();
        List<GwblT> gwbl_tPOs = new ArrayList<>();

        try {
            JSONObject inJson = JSONObject.parseObject(inDto.getString("_jsonStr_"));

            JSONObject data = inJson.getJSONObject("data");

            gwbh_tPO = CommonUtils.toJavaClass(data, GwbhT.class);

            gwbh_tPO.setGWBHENT(currentUser.getDBC());
            gwbh_tPO.setGWBHMODU(currentUser.getID());

            if (ToolUtil.isNotEmpty(data.getString("GWBH019"))) {
                //转成Timestamp 不然时间存入的时候是00:00:00
                Timestamp t1 = (Timestamp) TypeConvertUtil.convert(data.getString("GWBH019"), "Timestamp", "yyyy-MM-dd HH:mm:ss");
                gwbh_tPO.setGWBH019(t1.toLocalDateTime());
            }

            if (ToolUtil.isNotEmpty(data.getString("GWBH020"))) {
                Timestamp t2 = (Timestamp) TypeConvertUtil.convert(data.getString("GWBH020"), "Timestamp", "yyyy-MM-dd HH:mm:ss");
                gwbh_tPO.setGWBH020(t2.toLocalDateTime());
            }

            //订单明细
            Dto gwbkMaxDto = Dtos.newDto();
            gwbkMaxDto.put("GWBKENT", inDto.getString("ENT"));
            gwbkMaxDto.put("GWBKACTI", "Y");
            gwbkMaxDto.put("GWBKNAME", gwbh_tPO.getGWBHNAME());
            gwbkMaxDto.put("GWBKID", gwbh_tPO.getGWBHID());
            gwbkMaxDto.put("GWBKSEQ2", gwbh_tPO.getGWBHSEQ());
            List<Dto> gwbkMaxlist = gwat161Service.getGwbkseqMax(gwbkMaxDto);
            BigDecimal gwbkMaxNum;
            if (gwbkMaxlist.size() > 0 && ToolUtil.isNotEmpty(gwbkMaxlist.get(0))) {
                Dto maxDto = gwbkMaxlist.get(0);
                gwbkMaxNum = maxDto.getBigDecimal("GWBKSEQ");
            } else {
                gwbkMaxNum = BigDecimal.ZERO;
            }
            JSONArray orders = inJson.getJSONArray("orders");
            for (int i = 0; i < orders.size(); i++) {
                JSONObject item = orders.getJSONObject(i);
                GwbkT gwbk_tPO = new GwbkT();

                gwbk_tPO.setGWBKENT(currentUser.getDBC());
                gwbk_tPO.setGWBKNAME(gwbh_tPO.getGWBHNAME());
                gwbk_tPO.setGWBKID(data.getString("GWAK001"));
                gwbk_tPO.setGWBKSEQ(gwbkMaxNum.add(new BigDecimal(i + 1)));
                gwbk_tPO.setGWBKSEQ2(gwbh_tPO.getGWBHSEQ());

                gwbk_tPO.setGWBK001(item.getString("GWAM001"));
                gwbk_tPO.setGWBK002(item.getString("GWAM008"));
                gwbk_tPO.setGWBK003(item.getBigDecimal("GWCM003"));
                gwbk_tPO.setGWBK004(item.getBigDecimal("GWAP100"));
                gwbk_tPO.setGWBK005(gwbk_tPO.getGWBK003().multiply(gwbk_tPO.getGWBK004()));
                gwbk_tPO.setGWBK008(item.getString("GWBK008"));
                gwbk_tPO.setGWBK009(item.getString("GWBK009"));
                gwbk_tPO.setGWBKACTI("Y");
                gwbk_tPO.setGWBKDATE(LocalDate.now());
                gwbk_tPO.setGWBKUSER(currentUser.getID());
                gwbk_tPO.setGWBKORIG(currentUser.getORG_ID());

                gwbk_tPOs.add(gwbk_tPO);
            }

            //修改已经有的订单
            JSONArray oldorders = inJson.getJSONArray("oldorders");
            for (int i = 0; i < oldorders.size(); i++) {
                JSONObject item = oldorders.getJSONObject(i);
                GwbkT gwbk_tPO = new GwbkT();

                gwbk_tPO.setGWBKENT(currentUser.getDBC());
                gwbk_tPO.setGWBKNAME(gwbh_tPO.getGWBHNAME());
                gwbk_tPO.setGWBKID(data.getString("GWAK001"));
                gwbk_tPO.setGWBKSEQ(item.getBigDecimal("GWBKSEQ"));
                gwbk_tPO.setGWBKSEQ2(gwbh_tPO.getGWBHSEQ());

                gwbk_tPO.setGWBK001(item.getString("GWBK001"));
                gwbk_tPO.setGWBK002(item.getString("GWBK002"));
                gwbk_tPO.setGWBK003(item.getBigDecimal("GWBK003"));
                gwbk_tPO.setGWBK004(item.getBigDecimal("GWBK004"));
                gwbk_tPO.setGWBK005(gwbk_tPO.getGWBK003().multiply(gwbk_tPO.getGWBK004()));
                gwbk_tPO.setGWBK008(item.getString("GWBK008"));
                gwbk_tPO.setGWBK009(item.getString("GWBK009"));
                gwbk_tPO.setGWBKACTI("Y");
                gwbk_tPO.setGWBKDATE(LocalDate.now());
                gwbk_tPO.setGWBKUSER(currentUser.getID());
                gwbk_tPO.setGWBKORIG(currentUser.getORG_ID());

                gwbk_tPO1s.add(gwbk_tPO);
            }

            //评估项
            JSONArray pgxs = inJson.getJSONArray("pgx");
            if (pgxs.size() == 0) {
                delGwblT = new GwblT();
                delGwblT.setGWBLENT(currentUser.getDBC());
                delGwblT.setGWBLNAME(gwbh_tPO.getGWBHNAME());
                delGwblT.setGWBLID(data.getString("GWAK001"));
//                delGwblT.deleteById();//考虑放在事务中进行
            }
            for (int i = 0; i < pgxs.size(); i++) {
                JSONObject item = pgxs.getJSONObject(i);
                GwblT gwbl_tPO = new GwblT();
                gwbl_tPO.setGWBLENT(currentUser.getDBC());
                gwbl_tPO.setGWBLNAME(gwbh_tPO.getGWBHNAME());
                gwbl_tPO.setGWBLID(data.getString("GWAK001"));
                gwbl_tPO.setGWBLSEQ(new BigDecimal(i + 1));
                if (ToolUtil.isNotEmpty(item.getString("old"))) {
                    gwbl_tPO.setGWBL001(item.getString("GWBL001"));
                    gwbl_tPO.setGWBL002(item.getString("GWBL002"));
                } else {
                    gwbl_tPO.setGWBL001(item.getString("GWBC001"));
                    gwbl_tPO.setGWBL002(item.getString("GWBC002"));
                }

                gwbl_tPO.setGWBLACTI("Y");
                gwbl_tPO.setGWBLDATE(LocalDate.now());
                gwbl_tPO.setGWBLUSER(currentUser.getID());
                gwbl_tPO.setGWBLORIG(currentUser.getORG_ID());
                gwbl_tPOs.add(gwbl_tPO);
            }

            GwbeT gwbe_tPO = new GwbeT();
            gwbe_tPO.setGWBEENT(currentUser.getDBC());
            gwbe_tPO.setGWBE001(gwbh_tPO.getGWBHNAME());
            gwbe_tPO.setGWBE002(gwbh_tPO.getGWBH016());
            gwbe_tPO.setGWBE003(gwbh_tPO.getGWBH035());

            gwat161Service.gwat161Edit(currentUser, gwbh_tPO, gwbk_tPOs, gwbl_tPOs, gwbk_tPO1s, gwbe_tPO, delGwblT);

        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData();
    }

    /**
     * 获取经销商信息
     *
     * @return
     */
    @RequestMapping(value = "getJxs")
    public ResponseData getJxs(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        return new SuccessResponseData(gwat161Service.getJxs(inDto));
    }
//

    /**
     * 根据主题+ 客户获取 评估值
     *
     * @return
     * @author wenchuan.li on 2018/07/12 15:52
     */
    @RequestMapping(value = "getPgx")
    public ResponseData getPgx(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        inDto.put("GWBLENT", inDto.getString("ENT"));
        inDto.put("GWBLACTI", "Y");
        return new SuccessResponseData(gwat161Service.getPgx(inDto));
    }

    /**
     * 删除拜访记录
     * <p>
     * 拜访记录删除时要判断一下，如果是同一商机主题下最后一笔拜访记录删除，
     * 那么就要把该商机主题相关的子档都要删除（商品明细、关键评估项、竞争对手、拜访角色）
     * 如果是同一商机主题下非最后一笔拜访记录删除，只需删除本笔拜访记录
     *
     * @return
     * @author wenchuan.li on 2018/08/28 11:07
     */
    @RequestMapping(value = "delBfjl")
    public ResponseData delBfjl(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        try {
            inDto.put("GWBHENT", inDto.getString("ENT"));
            inDto.put("GWBHACTI", "Y");

            List<Dto> bfjlList = gwat161Service.countBfjl(inDto);

            gwat161Service.delBfjl(inDto, bfjlList.size());
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData();
    }

    /**
     * 获取客户列表
     *
     * @return
     */
    @RequestMapping(value = "getGwakList")
    public ResponseData getGwakList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser user = wapper.getSysUser();
        inDto.put("GWAKENT", inDto.getString("ENT"));
        inDto.put("GWAKACTI", "Y");

        Dto gwb001 = gwbService.getGwb001(user, inDto);
        inDto.put("CUSTOMERS", gwb001.get("CUSTOMERS"));

        return new SuccessResponseData(gwat161Service.getGwakList(wapper.getPage(), inDto));
    }
//

    /**
     * 根据所属组织获取拜访内容填写分类
     *
     * @return
     * @author wenchuan.li on 2018/09/27 17:36
     */
    @RequestMapping(value = "getGwbfListByzz")
    public ResponseData getGwbfListByzz(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();

        inDto.put("GWBF001", currentUser.getORG_ID());
        inDto.put("GWBFENT", inDto.getString("ENT"));
        inDto.put("GWBFACTI", "Y");

        return new SuccessResponseData(gwat161Service.getGwbfListByzz(inDto));
    }
}
